﻿using GrainManage.Entities.Price;
using System;

namespace GrainManage.Server.Business
{
    public class PriceBusiness : BaseBusiness, GrainManage.Interface.IPrice
    {
        //************************************************POST************************************************

        public OutputGetByID GetByID(InputGetByID input)
        {
            var result = new OutputGetByID();
            try
            {
                var service = ObjectFactory.PriceService;
                result.Price = service.Get(input.PriceID, input.Authentication.UserName);
                if (result.Price == null)
                {
                    SetResponse(StatusCode.NoData, input, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputGetRecentPriceByGrain GetRecentPriceByGrain(InputGetRecentPriceByGrain input)
        {
            var result = new OutputGetRecentPriceByGrain();
            try
            {
                var service = ObjectFactory.PriceService;
                result.Price = service.GetRecentPriceByGrain(input.PriceType, input.Grain, input.Authentication.UserName);
                if (result.Price == null)
                {
                    SetResponse(StatusCode.NoData, input, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputSearchRecentPrice SearchRecentPrice(InputSearchRecentPrice input)
        {
            var result = new OutputSearchRecentPrice();
            try
            {
                var service = ObjectFactory.PriceService;
                result.Prices = service.SearchRecentPrice(input.PriceFilter, input, result, input.Authentication.UserName);
                if (result.Prices.Count == 0)
                {
                    SetResponse(StatusCode.NoData, input, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputSearchPrice SearchPrice(InputSearchPrice input)
        {
            var result = new OutputSearchPrice();
            try
            {
                var service = ObjectFactory.PriceService;
                result.Prices = service.SearchPrice(input.PriceFilter, input, result, input.Authentication.UserName);
                if (result.Prices.Count == 0)
                {
                    SetResponse(StatusCode.NoData, input, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputInsert Insert(InputInsert input)
        {
            var result = new OutputInsert();
            try
            {
                var service = ObjectFactory.PriceService;
                input.Price.Creator = input.Authentication.UserName;
                result.PriceID = service.Insert(input.Price);
                if (result.PriceID > 0)
                {
                    SetResponse(StatusCode.Success, input, result);
                }
                else
                {
                    SetResponse(StatusCode.InsertFailed, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputUpdate Update(InputUpdate input)
        {
            var result = new OutputUpdate();
            try
            {
                input.Price.Creator = input.Authentication.UserName;
                var service = ObjectFactory.PriceService;
                if (service.Update(input.Price) > 0)
                {
                    SetResponse(StatusCode.Success, input, result);
                }
                else
                {
                    SetResponse(StatusCode.UpdateFailed, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputDelete Delete(InputDelete input)
        {
            var result = new OutputDelete();
            try
            {
                var service = ObjectFactory.PriceService;
                if (service.Delete(input.PriceID, input.Authentication.UserName) > 0)
                {
                    SetResponse(StatusCode.Success, input, result);
                }
                else
                {
                    SetResponse(StatusCode.DeleteFailed, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputBatchInsert BatchInsert(InputBatchInsert input)
        {
            var result = new OutputBatchInsert();
            try
            {
                var service = ObjectFactory.PriceService;
                result.PriceIDs = service.Insert(input.Prices);
                if (!result.PriceIDs.Contains(0))
                {
                    SetResponse(StatusCode.Success, input, result);
                }
                else
                {
                    SetResponse(StatusCode.BatchInsertFailed, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputBatchUpdate BatchUpdate(InputBatchUpdate input)
        {
            var result = new OutputBatchUpdate();
            try
            {
                var service = ObjectFactory.PriceService;
                result.InvalidPriceIDs = service.Update(input.Prices);
                if (result.InvalidPriceIDs.Count == 0)
                {
                    SetResponse(StatusCode.Success, input, result);
                }
                else
                {
                    SetResponse(StatusCode.BatchUpdateFailed, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputBatchDelete BatchDelete(InputBatchDelete input)
        {
            var result = new OutputBatchDelete();
            try
            {
                var service = ObjectFactory.PriceService;
                result.InvalidPriceIDs = service.Delete(input.PriceIDs, input.Authentication.UserName);
                if (result.InvalidPriceIDs.Count == 0)
                {
                    SetResponse(StatusCode.Success, input, result);
                }
                else
                {
                    SetResponse(StatusCode.BatchDeleteFailed, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }
    }
}
